package com.android.phoenix.activity;

import java.util.ArrayList;
import java.util.List;

import com.android.phoenix.R;
import com.android.phoenix.activity.discovery.BycecleActivity;
import com.android.phoenix.activity.discovery.RankListActivity;
import com.android.phoenix.activity.discovery.SafetyActivity;
import com.android.phoenix.activity.location.OfflineMapActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DiscoveryActivity extends BaseActivity {
	
	private GridView gridDiscovery;
	private ImageView imgBack;
	private BaseAdapter mAdapter;
	private List<String> mList = new ArrayList<String>();
	private static final int FIRST_ITEM = 0;
	private static final int SECOND_ITEM = 1;
	private static final int THREE_ITEM = 2;
	private static final int FOURTH_ITEM = 3;
	private static final int mIteImage[] = {R.drawable.img_safety, R.drawable.img_rank, 
		R.drawable.img_activity, R.drawable.img_offline_map};
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acti_discovery);
		
		gridDiscovery = (GridView) findViewById(R.id.gridDiscovery);
		mAdapter = new GridDiscoveryAdapter();
		gridDiscovery.setAdapter(mAdapter);
		
		gridDiscovery.setOnItemClickListener(new DiscoveryGridListener());
		
		imgBack = (ImageView) findViewById(R.id.imgBack);
		imgBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		mList.add("安全");
		mList.add("排行榜");
		mList.add("活动");
		mList.add("离线地图");
		
	}
	
	private class GridDiscoveryAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			convertView = LayoutInflater.from(DiscoveryActivity.this).inflate(R.layout.acti_discovery_item_gird, null);
			TextView tvGridName = (TextView) convertView.findViewById(R.id.tvGridName);
			tvGridName.setText(mList.get(position));
			ImageView imgTitle = (ImageView) convertView.findViewById(R.id.imgTitle);
			imgTitle.setImageResource(mIteImage[position]);
			return convertView;
		}
		
	}
	
    //	 对GridView的监听
	class DiscoveryGridListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			//跳转到安全界面
			case FIRST_ITEM:
				jump(SafetyActivity.class);
				break;
			// 跳转到排行榜
			case SECOND_ITEM:
				jump(RankListActivity.class);
				break;
			// 跳转到活动
			case THREE_ITEM:
				jump(BycecleActivity.class);
				break;
			// 跳转到离线地图
			case FOURTH_ITEM:
				jump(OfflineMapActivity.class);
				break;
			}
		}
	}
	
	private void jump(Class cls){
		Intent intent = new Intent();
		intent.setClass(this, cls);
		startActivity(intent);
	}
	
}

